package com.me.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;

public class ATag extends SimpleTagSupport {
	private String controller;
	private String method;
	private String route;
	private String jsp_class;

	@Override
	public void doTag() throws JspException, IOException {
		PageContext page = ((PageContext) this.getJspContext());
		HttpServletRequest req = (HttpServletRequest)page.getRequest();
		JspWriter out = page.getOut();
		StringWriter sw = new StringWriter();
		getJspBody().invoke(sw);

		if (controller == null || controller == "") {
			controller = req.getRequestURI().split("/")[1];
		}

		out.print(
				String.format("<a href='/%s/%s/%s' %s>%s</a>",
						controller.toLowerCase(), method,
						route == null ? "" : route,
						jsp_class == null  ? "" : jsp_class,
						sw
				)
		);
	}

	public String getController() {
		return controller;
	}

	public void setController(String controller) {
		this.controller = controller;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getRoute() {
		return route;
	}

	public void setRoute(String route) {
		this.route = route;
	}

	public String getJsp_class() {
		return jsp_class;
	}

	public void setJsp_class(String jsp_class) {
		this.jsp_class = jsp_class;
	}
}
